package com.ht.medicaltreatment.view

import android.content.Context
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.gym.baselib.base.BaseActivity
import com.gym.baselib.base.BaseFragment
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.hjq.http.request.PostRequest
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.api.RetrofitUtils
import com.ht.medicaltreatment.bean.FloorBean
import com.ht.medicaltreatment.bean.IceboxBean
import com.ht.medicaltreatment.bean.IceboxDrawerBean
import com.ht.medicaltreatment.bean.IceboxItemListBean
import com.ht.medicaltreatment.bean.IceboxLocationBean
import com.ht.medicaltreatment.http.api.FloorAPI2
import com.xgst.driverlesscars.api.ApiService
import com.xgst.lib.http.ApiResult
import kotlinx.android.synthetic.main.dialog_choose_layout.iceboxNumber
import kotlinx.android.synthetic.main.dialog_choose_layout.llProgress
import kotlinx.android.synthetic.main.dialog_choose_layout.mbNegative
import kotlinx.android.synthetic.main.dialog_choose_layout.mbPositive
import kotlinx.android.synthetic.main.dialog_choose_layout.tvDrawer
import kotlinx.android.synthetic.main.dialog_choose_layout.tvIcebox
import kotlinx.android.synthetic.main.dialog_choose_layout.tvLocation
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

/**
 * 创建样本，选择样本盒位置
 */
class SampleBoxLocationDialog(context: Context):BaseDialog(context) {

    private val mActivity = context
    override fun getLayoutRes() = R.layout.dialog_choose_location_layout

    private var itemDialog = ItemListDialog(context)
    private val locationArr = intArrayOf(-1,-1,-1,-1)
    private val mApi = RetrofitUtils.initRetrofit(ApiService::class.java)
    init {
        setCancelable(false)

        mbNegative.setOnClickListener { dismiss() }
    }

    fun show(onClick:(String,String) -> Unit) {
        super.show()
        //选择设备
        tvIcebox.setOnClickListener {
            llProgress.visibility = View.VISIBLE
            iceboxData({
                val list = mutableListOf<IceboxItemListBean>()
                it.forEach { bean->
                    list.add(IceboxItemListBean(bean.id,bean.name,""))
                }
                llProgress.visibility = View.GONE
                itemDialog.show(list,{name,id ->
                    reset()
                    tvIcebox.text = name
                    locationArr[0] = id
                })
            })
        }

        //层数
        iceboxNumber.setOnClickListener {
            if(locationArr[0] == -1){
                ToastUtils.showShort("请先选择设备")
                return@setOnClickListener
            }
            llProgress.visibility = View.VISIBLE
            var postRequest: PostRequest
            if(mActivity is BaseActivity){
                LogUtils.i("BaseActivity")
                postRequest = EasyHttp.post(mActivity)
            }else if(mActivity is BaseFragment){
                LogUtils.i("BaseFragment")
                postRequest = EasyHttp.post(mActivity as BaseFragment)
            }else{
                return@setOnClickListener
            }
            postRequest.api(FloorAPI2(locationArr[0]))
                .request(object : OnHttpListener<FloorBean>{
                    override fun onHttpSuccess(result: FloorBean?) {
                        val list = mutableListOf<IceboxItemListBean>()
                        result?.data?.forEach { item->
                            list.add(IceboxItemListBean(item.id,"${item.location}",item.disabled))
                        }
                        llProgress.visibility = View.GONE
                        itemDialog.show(list,{name,id ->
                            locationArr[2] = -1
                            locationArr[3] = -1
                            tvDrawer.text = ""
                            tvLocation.text = ""

                            iceboxNumber.text = name
                            locationArr[1] = id
                        })
                    }

                    override fun onHttpFail(e: Exception) {
                        ToastUtils.showShort(e.message)
                    }
                })
        }

        //抽屉
        tvDrawer.setOnClickListener {
            if(locationArr[1] == -1){
                ToastUtils.showShort("请先选择层数")
                return@setOnClickListener
            }
            llProgress.visibility = View.VISIBLE
            iceboxDrawer({
                val list = mutableListOf<IceboxItemListBean>()
                it.forEach { bean->
                    list.add(IceboxItemListBean(bean.id,"${bean.location}",bean.disabled))
                }
                llProgress.visibility = View.GONE
                itemDialog.show(list,{name,id ->

                    locationArr[3] = -1
                    tvLocation.text = ""

                    tvDrawer.text = name
                    locationArr[2] = id
                })
            })
        }

        //位置
        tvLocation.setOnClickListener {
            if(locationArr[2] == -1){
                ToastUtils.showShort("请先选择抽屉")
                return@setOnClickListener
            }
            llProgress.visibility = View.VISIBLE
            iceboxLocation({
                val list = mutableListOf<IceboxItemListBean>()
                it.forEach { bean->
                    list.add(IceboxItemListBean(bean.id,"${bean.location}",""))
                }
                llProgress.visibility = View.GONE
                itemDialog.show(list,{name,id ->
                    tvLocation.text = "$name"
                    locationArr[3] = id
                })
            })
        }

        mbPositive.setOnClickListener {
            if(locationArr.find { it == -1 } != null){
                ToastUtils.showShort("请填写完整")
                return@setOnClickListener
            }
            onClick("${tvIcebox.text}-${iceboxNumber.text}-${tvDrawer.text}-${tvLocation.text}","${locationArr[3]}")
            dismiss()
        }
    }

    private fun reset() {
        locationArr[0] = -1
        locationArr[1] = -1
        locationArr[2] = -1
        locationArr[3] = -1
        iceboxNumber.text = ""
        tvDrawer.text = ""
        tvLocation.text = ""
    }

    //冰箱数据
    fun iceboxData(onResult:(MutableList<IceboxBean>)->Unit){
        MainScope().launch {
            val bean = mApi.iceboxData()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> {
                    llProgress.visibility = View.GONE
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //抽屉
    fun iceboxDrawer(onResult:(MutableList<IceboxDrawerBean>)->Unit){
        MainScope().launch {
            val bean = mApi.iceboxDrawer2("${locationArr[1]}")
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> {
                    llProgress.visibility = View.GONE
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //位置
    fun iceboxLocation( onResult:(MutableList<IceboxLocationBean>)->Unit){
        MainScope().launch {
            val bean = mApi.iceboxLocation2("${locationArr[2]}")
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> {
                    llProgress.visibility = View.GONE
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }
}